﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotvaBot.Configuration;
using BotvaBot.Configuration.Personaj;
using BotvaBot;
using BotvaBot.RequestChains;
using BotvaBot.BusinessLayer.Enums;
using BotvaBot.RequestChains.Base;

namespace IgromanHelper.WinForm
{
    public partial class MainForm : Form
    {
        protected PersonajCfg CurrentPersonaj { get { return cmbPersonaj.SelectedItem as PersonajCfg; } }


        
        public MainForm()
        {
            InitializeComponent();
        }


        //protected List<int> BitAmounts = new List<int> { 0, 125, 250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000, 256000, 512000, 1024000, 2048000, 4096000 };
        protected List<int> BitAmounts = new List<int> { 0, 7, 15, 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000, 256000, 512000, 1024000, 2048000, 4096000 };

        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            cmbPersonaj.DisplayMember = "Name";
            cmbPersonaj.DataSource = Config.Custom.PersonajSet.Items;
            cmbPersonaj_SelectedIndexChanged(cmbPersonaj, null);
            cmbPersonaj.SelectedIndexChanged += cmbPersonaj_SelectedIndexChanged;

            cmbSoftKoefficient.DataSource = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            cmbBeginingBit.DataSource = BitAmounts.ToList();
            cmbMaximumBit.DataSource = BitAmounts.ToList();
        }

        private void cmbPersonaj_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtEmail.Text = CurrentPersonaj.Email;
            txtPassword.Text = CurrentPersonaj.Password;

            RefreshMoney();
        }

        private void btnSingleGame_Click(object sender, EventArgs e)
        {
            TakIlTakVariants playingVariant = rbtnGt.Checked ? TakIlTakVariants.GreaterThan : TakIlTakVariants.LessThan;
            int moneyAmount = (int)cmbBeginingBit.SelectedItem;
            
            bool? isWin = TryPlay(CurrentPersonaj, moneyAmount, playingVariant);
            string result = isWin == null ? "UNDEFINED" : isWin.Value.ToString().ToUpper();
            MessageBox.Show(result);

            RefreshMoney();
        }

        private void btnGameSession_Click(object sender, EventArgs e)
        {
            TakIlTakVariants playingVariant = rbtnGt.Checked ? TakIlTakVariants.GreaterThan : TakIlTakVariants.LessThan;
            int beginBit = (int)cmbBeginingBit.SelectedItem;
            int maxBit = (int)cmbMaximumBit.SelectedItem;
            
            bool? result = TryPlaySession(CurrentPersonaj, beginBit, maxBit, playingVariant);
            string str = result == null ? "UNDEFINED" : result.Value.ToString().ToUpper();
            MessageBox.Show(str);

            RefreshMoney();
        }

        protected int RefreshMoney()
        {
            PersonajInfo pi = new PersonajInfo(CurrentPersonaj);
            pi = pi.GetInfo();
            lbCurrentGoldAmount.Text = pi.Money.ToString();
            return pi.Money;
        }

        protected int RefreshMoney(BotvaChainContext context)
        {
            PersonajInfo pi = new PersonajInfo(CurrentPersonaj, context);
            pi = pi.GetInfo();
            lbCurrentGoldAmount.Text = pi.Money.ToString();
            return pi.Money;
        }

        protected bool? TryPlay(PersonajCfg personaj, int moneyAmount, TakIlTakVariants playingVariant)
        {
            LoginChain loginCh = new LoginChain();
            loginCh.Personaj = CurrentPersonaj;
            loginCh.RunChain();

            var context = loginCh.Context;

            PlayTakIlTakChain ch = new PlayTakIlTakChain(context);
            ch.Personaj = personaj;
            ch.WithInheritedContext = true;
            ch.MoneyAmount = moneyAmount;
            ch.PlayingVariant = playingVariant;
            ch.RunChain();
            return ch.IsWin;
        }

        protected bool? TryPlaySession(PersonajCfg personaj, int beginBit, int maxBit, TakIlTakVariants playingVariant)
        {
            int bit = beginBit;
            bool isWin = false;
            bool? result = null;
            while ((!isWin) && (bit <= maxBit))
            {
                result = TryPlay(CurrentPersonaj, bit, playingVariant);
                isWin = result == null || result.Value;
                bit *= 2;
            }

            return result;
        }

        private void btnCalculateBeginingBit_Click(object sender, EventArgs e)
        {
            int sk = (int)cmbSoftKoefficient.SelectedItem;
            if(sk == 0)
                return;

            int money = RefreshMoney();
            if (money <= BitAmounts.First())
            {
                cmbBeginingBit.SelectedIndex = 0;
                return;
            }

            int bit = money;
            for (int i = 0; i < sk; i++)
            {
                bit = (int)((decimal)bit / 2.0M);
            }

            int standardBit = BitAmounts.First();
            for (int i = 1; i < BitAmounts.Count; i++)
            {
                if (BitAmounts[i] < bit)
                    standardBit = BitAmounts[i];
            }

            int index = BitAmounts.FindIndex(ba => ba == standardBit);

            cmbBeginingBit.SelectedIndex = index;
        }

        private void btnCalculateMaximumBit_Click(object sender, EventArgs e)
        {
            int sk = (int)cmbSoftKoefficient.SelectedItem;
            if (sk == 0)
                return;

            int beginingBit = (int)cmbBeginingBit.SelectedItem;
            if (beginingBit == 0)
                return;

            int maxBit = beginingBit;
            for (int i = 1; i < sk; i++)
                maxBit *= 2;

            int index = BitAmounts.FindIndex(ba => ba == maxBit);

            cmbMaximumBit.SelectedIndex = index;
        }

        private void cmbSoftKoefficient_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sk = (int)cmbSoftKoefficient.SelectedItem;
            if (sk == 0)
                lbPossibility.Text = "0 %";

            decimal possibility = 1.0M;
            for (int i = 0; i < sk; i++)
                possibility = (decimal)((decimal)possibility / 2.0M);

            possibility *= 100.0M;

            lbPossibility.Text = string.Format("{0} %", possibility);
        }

        private void btnGameSessionSet_Click(object sender, EventArgs e)
        {
            int sessionCount = (int)spinGameSessionSetCount.Value;

            TakIlTakVariants playingVariant = rbtnGt.Checked ? TakIlTakVariants.GreaterThan : TakIlTakVariants.LessThan;
            int beginBit = (int)cmbBeginingBit.SelectedItem;
            int maxBit = (int)cmbMaximumBit.SelectedItem;
            
            bool? isWin = null;
            for (int i = 0; i < sessionCount; i++)
            {
                isWin = TryPlaySession(CurrentPersonaj, beginBit, maxBit, playingVariant);
                if (isWin == null || (!isWin.Value))
                    break;
            }

            string str = isWin == null ? "UNDEFINED" : isWin.Value.ToString().ToUpper();

            RefreshMoney();
            
            MessageBox.Show(str);
        }

    }
}
